package com.rr.service.impl;

import com.rr.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuangcanrun
 * @Date 2022/05/26/21:23
 * @description
 **/

@Service
@Slf4j
public class TestServiceImpl implements TestService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Async
    @Override
    public void normalSyncMethod() {
        log.info("线程:{}开始睡眠3秒，", Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("线程:{}睡眠3秒结束，", Thread.currentThread().getName());
    }

    @Async(value = "threadPoolExecutor")
    @Override
    public void syncMethodWithExecutor() {
        log.info("线程:{}开始睡眠3秒，", Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("线程:{}睡眠3秒结束，", Thread.currentThread().getName());
    }

    @Override
    public void syncMethodWithExecutor1() {
        threadPoolExecutor.submit(() -> {
            log.info("线程:{}开始睡眠3秒，", Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("线程:{}睡眠3秒结束，", Thread.currentThread().getName());
        });
    }
}
